package com.lcz.googleplay.utils;

import android.os.Environment;

import java.io.File;

/**
 * Created by hp on 2016/10/26.
 */

public class FileUtils {
    public static final String CACHE = "cache";
    public static final String ICON = "icon";
    public static final String ROOT = "GooglePlay";


    //文件缓存路径
    public static File getCacheDir() {
        return getDir(CACHE);
    }

    //图片缓存路径
    public static File getIconDir() {
        return getDir(ICON);
    }

    /**
     * 缓存文件目录
     *
     * @param cache
     * @return
     */
    public static File getDir(String cache) {
        StringBuilder path = new StringBuilder();
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            path.append(Environment.getExternalStorageDirectory().getAbsolutePath())
                    .append("/").append(ROOT).append("/")
                    .append(cache);
        } else {//没有SD卡就保存在项目所在位置
            File dir = UIUtils.getActivity().getCacheDir();
            path.append(dir.getAbsolutePath())
                    .append("/").append(cache);
        }
        File file = new File(path.toString());
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();
        }

        return file;
    }
}
